/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2013-08-15     WangShun  the first version
 */

#include "aht10x.h"

extern int32_t aos_debug_printf(const char *fmt, ...);
void aht10(int argc, char *argv[])
{
    float temp, humi;
    int id = 0;
    char* usr_str;
    static aht10_device_t dev = NULL;
    uint8_t temp_H, temp_L; 
    uint8_t humi_H, humi_L; 

    if (argc > 1)
    {
        if (!strcmp(argv[1], "probe"))
        {
            if(argc > 2)
            {
                /* initialize the sensor when first probe */
                if (dev)
                {
                    aht10_deinit(dev);
                }

                int len = strlen(argv[2]);
                if (len > 3)
                {
                    strtok(argv[2],"c");
                    usr_str = strtok(NULL,"c");
                    id = atoi(usr_str);
                    dev = aht10_init(argv[2], id);
                }
                else
                {
                    aos_debug_printf("Please enter the correct device parameters, such as' i2c1 '\r\n");
                }
            }
            else
            {
                aos_debug_printf("aht10 probe <dev_name>   - probe sensor by given name\n");
            }            
        }
        else if(!strcmp(argv[1], "read"))
        {
            if(dev)
            {
                temp = aht10_read_temperature(dev);
                humi = aht10_read_humidity(dev);

                temp_H = (temp*100)/100;
                temp_L = (uint8_t)(temp*100)%100;

                humi_H = (humi*100)/100;
                humi_L = (uint8_t)(humi*100)%100;
                aos_debug_printf("temp = %d.%d, humi = %d.%d \r\n ", temp_H, temp_L, humi_H, humi_L);
                aos_msleep(100);
         
            }
        }
        else
        {
            aos_debug_printf("Unknown command. Please enter 'aht10' for help\n");
        } 
    }
    else
    {
        aos_debug_printf("Usage:\n");
        aos_debug_printf("aht10 probe <dev_name>   - probe sensor by given name\n");
        aos_debug_printf("aht10 read               - read sensor AHT10 data\n");
    }   
}
ALIOS_CLI_CMD_REGISTER(aht10, aht10, aht10 sensor function);
